package cn.gov.anhai.filter;

import cn.gov.anhai.controller.TourismController;
import cn.gov.anhai.utils.ApiResult;
import cn.gov.anhai.utils.LoginUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

@Aspect
@Component
public class AuthAspect {
    private Logger logger = LoggerFactory.getLogger(TourismController.class);

    /**
     * 定义了一个切点
     * 这里的路径填自定义注解的全路径
     */
    @Pointcut("@annotation(cn.gov.anhai.filter.Auth)")
    public void authCut() {

    }

    @Around("authCut()")
    public Object trackInfo(ProceedingJoinPoint pjp) throws Throwable {

        ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = sra.getRequest();
        Long uid = LoginUtil.getToken(request);
        if (uid == null) {
            return ApiResult.fail("还未登录或已登出");
        }
        return pjp.proceed();
    }

}
